package com.youlai.boot.system.controller;

import com.youlai.boot.common.result.Result;
import com.youlai.boot.common.result.ResultCode;
import com.youlai.boot.system.model.QB.Document;
import com.youlai.boot.system.model.QB.Equipment;
import com.youlai.boot.system.service.EquipmentService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/v1/equipment")
public class EquipmentController {
    /**
     * 设备设施SMP
     */
    @Autowired
    private EquipmentService equipmentService;

    @PostMapping("/save")
    public Result<?> saveEquipment(@Valid @RequestBody Equipment equipment){
        return Result.judge(equipmentService.saveequipment(equipment),"添加成功","添加失败");
    }
    @PostMapping("/all")
    public Result<List<Equipment>> getAllEquipment(){
        List<Equipment> documents = equipmentService.getAllEquipment();
        Result<List<Equipment>> result;
        if (!documents.isEmpty()){
            result  = Result.success(documents);
            result.setMessage("查询设备成功");
            result.setCode(ResultCode.SUCCESS.getCode());
        }else {
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR);
            result.setMessage("查询失败，请重新查询");
        }
        return result;
    }
    @DeleteMapping("/{id}")
    public Result<?> deleteEquipment(@PathVariable Integer id){
        return Result.judge(equipmentService.deletEquipmentById(id),"删除成功","删除失败");
    }
}
